﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HySoft.Cash.BaseClass;
using System.Configuration;
using Huayou.Common;
using PayRoller.Code;
using System.Web.SessionState;
using HySoft.Cash.Business;
using HySoft.Cash;
using HySoft.Cash.Structure;
using HiLand.Utility.Data;
using Huayou.Utils;
using System.Net.Mail;

namespace PayRoller.Manage
{
    public partial class PopupWindow : ParentPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                base.ParamValue = base.UnPack();
                this.txtParamstr.Text = base.PackPart(base.ParamValue);
                string str = base.GetValue("pageno");
                if (str != null)
                {
                    this.dgdList.CurrentPageIndex = Convert.ToInt32(str) - 1;
                }
                this.dgdList.DataBind();
            }
            else
            {
                base.ParamValue = base.UnPackPart(this.txtParamstr.Text);
            }
        }

        protected void dgdList_DataBinding(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            string commandText = string.Format("select * from tOrgInfo where IsKnown={0} order by Sid desc", (int)OrgInfoKnowns.UnKnown);

            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["SQLConnString"], CommandType.Text, commandText).Tables[0];
            CommonBasePage.SetPage(this.dgdList, dt);
            base.AddValue("pageno", Convert.ToString((int)(this.dgdList.CurrentPageIndex + 1)));
            this.txtParamstr.Text = base.PackPart(base.ParamValue);
        }

        protected void dgdList_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            if (itemType == ListItemType.AlternatingItem || itemType == ListItemType.Item || itemType == ListItemType.SelectedItem)
            {
                DataRowView drv = e.Item.DataItem as DataRowView;
                int currentOrgID = Convert.ToInt32(drv["SID"]);

                Label hlID = e.Item.FindControl("hlID") as Label;
                if (hlID != null)
                {
                    hlID.Text = currentOrgID.ToString();
                }

                HyperLink hlClientName = e.Item.FindControl("hlClientName") as HyperLink;
                if (hlClientName != null)
                {
                    hlClientName.Text = drv["OrgName"].ToString();
                }

                Label hlStatus = e.Item.FindControl("hlStatus") as Label;
                if (hlStatus != null && Convert.IsDBNull(drv["IsValid"]) == false)
                {
                    OrginfoStatuses status = (OrginfoStatuses)Convert.ToInt32(drv["IsValid"]);
                    hlStatus.Text = TorginfoBN.GetOrgStatusText(status);
                }

                Label hlRegDate = e.Item.FindControl("hlRegDate") as Label;
                if (hlRegDate != null && Convert.IsDBNull(drv["RegDate"]) == false)
                {
                    hlRegDate.Text = Convert.ToDateTime(drv["RegDate"]).ToString(AppSettings.DateFormatAU, AppSettings.CurrentCulture);
                }

                Label hlExpCreditLimit = e.Item.FindControl("hlExpCreditLimit") as Label;
                if (hlExpCreditLimit != null && Convert.IsDBNull(drv["ExpCreditLimit"]) == false)
                {
                    hlExpCreditLimit.Text = drv["ExpCreditLimit"].ToString();
                }

                LinkButton libOperate = e.Item.FindControl("libOperate") as LinkButton;
                if (libOperate != null)
                {
                    libOperate.CommandArgument = currentOrgID.ToString();
                    //OrgInfoKnowns knownStatus = (OrgInfoKnowns)Convert.ToInt32(drv["IsKnown"]);
                    //libOperate.Text = TorginfoBN.GetOrgKnownText(knownStatus);
                }
            }
        }

        protected void dgdList_ItemCreated(object sender, DataGridItemEventArgs e)
        {
            ListItemType listItemType = e.Item.ItemType;
            if (listItemType == ListItemType.Item || listItemType == ListItemType.AlternatingItem)
            {
                LinkButton libSendStatus = e.Item.FindControl("libOperate") as LinkButton;
                if (libSendStatus != null)
                {
                    libSendStatus.Click += new EventHandler(libSendStatus_Click);
                }
            }
        }

        void libSendStatus_Click(object sender, EventArgs e)
        {
            int orgID = 0;
            LinkButton t = sender as LinkButton;
            if (t != null)
            {
                int.TryParse(t.CommandArgument, out orgID);
            }

            if (orgID > 0)
            {
                String connString = AppSettings.ConnectionString;
                string commString = string.Format("UPDATE [tOrgInfo]   SET [IsKnown] = {0} WHERE [SID]={1}", (int)OrgInfoKnowns.Known, orgID);
                SqlHelper.ExecuteNonQuery(connString, CommandType.Text, commString);

                this.dgdList.DataBind();
            }
        }

        protected void dgdList_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.dgdList.CurrentPageIndex = e.NewPageIndex;
            this.dgdList.DataBind();
        }
    }
}
